package com.ldy.core.common;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 通用异常处理
 * @author lindeyi
 *
 */
@ControllerAdvice
public class GlobalExceptionHandler {
	private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

//	@ResponseBody
	@ExceptionHandler
	public ResponseEntity<Result> globalException(Exception e, HttpServletResponse response) {
		logger.info("GlobalExceptionHandler: {}", e);
		 HttpHeaders headers = new HttpHeaders();
	     headers.add("Content-Type", MediaType.APPLICATION_JSON_UTF8_VALUE);
		 return new ResponseEntity<>(Result.failure(e.getMessage()),headers, HttpStatus.OK);
	}
	
	@ResponseBody
	@ExceptionHandler
	public Result loginException(LoginException e) {
		logger.info("loginException: {}", e);
		return Result.failure(Result.UNLOGIN, e.getMessage());
	}

}
